#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
##    Copyright (C) 2005 manatlan manatlan[at]gmail(dot)com
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##


from common.template import Template
import urllib
import StringIO
from meteo import getDays
from datetime import datetime
__title__=u"Météo"
__order__=4000
__author__ ="manatlan"
__about__  ="affiche la meteo suivant un departement"

class main:
    def index(self, dep = None):
        if dep:
            ddays = getDays(int(dep))
            ld = ddays.keys()
            ld.sort()
            days=[]
            for i in ld:
                a,b,c = ddays[i]
                d=i.strftime("%A %d %B")
                if i == datetime.now().date():
                    d="<u>%s</u>"%d
                days.append( (d,a,b,c) )
            return Template({"BACK":"index"})
        else:
            days=[]
            return Template({"BACK":"/index"})
        

    def image(self, dep,jo=""):
        tmp = urllib.urlopen("http://perso0.free.fr/cgi-bin/meteo.pl?dep=%s" % dep)
        chr = tmp.read(1024)
        output = StringIO.StringIO()
        while chr:
            output.write(chr)
            chr = tmp.read(1024)
        
        tmp.close()
        return 'image/gif',output.getvalue()

if __name__ == "__main__":
    print getDays(67)
